package com.ytjj.qmyx.mall.service.impl;

import com.ytjj.qmyx.mall.mapper.GroupBuyInfoMapper;
import com.ytjj.qmyx.mall.mapper.GroupBuyUserRelMapper;
import com.ytjj.qmyx.mall.model.GroupBuyInfoExample;
import com.ytjj.qmyx.mall.model.GroupBuyUserRel;
import com.ytjj.qmyx.mall.model.GroupBuyUserRelExample;
import com.ytjj.qmyx.mall.model.response.groupbuy.ProcessGroupBuyListResponse;
import com.ytjj.qmyx.mall.service.YxGroupBuyInfoService;
import com.ytjj.qmyx.mall.service.YxGroupBuyUserRelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class YxGroupBuyUserRelServiceImpl implements YxGroupBuyUserRelService {
    @Resource
    public GroupBuyUserRelMapper groupBuyUserRelMapper;

    @Override
    public boolean insert(GroupBuyUserRel userRel) {
        int result = groupBuyUserRelMapper.insertSelective(userRel);
        return result > 0 ? true : false;
    }

    @Override
    public GroupBuyUserRel getByOrderNo(String orderNo) {
        GroupBuyUserRelExample example = new GroupBuyUserRelExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        List<GroupBuyUserRel> rels = groupBuyUserRelMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(rels)) {
            return null;
        }
        return rels.get(0);
    }

    @Override
    public boolean update(GroupBuyUserRel userRel) {
        return groupBuyUserRelMapper.updateByPrimaryKeySelective(userRel) > 0 ? true : false;
    }

    @Override
    public List<GroupBuyUserRel> queryByGroupId(Integer groupId) {
        GroupBuyUserRelExample example = new GroupBuyUserRelExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        List<GroupBuyUserRel> rels = groupBuyUserRelMapper.selectByExample(example);
        return rels;
    }

    @Override
    public GroupBuyUserRel getByOrderNoAndUserId(String orderNo, Integer userId) {
        GroupBuyUserRelExample example = new GroupBuyUserRelExample();
        example.createCriteria().andOrderNoEqualTo(orderNo).andUserIdEqualTo(userId);
        List<GroupBuyUserRel> rels = groupBuyUserRelMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(rels)) {
            return null;
        }
        return rels.get(0);
    }

    /**
     * 检查是否参与过团
     * @param productId
     * @param userId
     * @return
     */
    public boolean checkIsBuy(Integer productId, Integer userId) {
        Integer groupBuyNum = groupBuyUserRelMapper.queryGroupBuyInfo(productId, userId);
        if (0 < groupBuyNum) {
            return true;
        }
        return false;
    }

    @Override
    public List<GroupBuyUserRel> queryCurUserJoinGroupId(Integer userId) {
        GroupBuyUserRelExample example = new GroupBuyUserRelExample();
        example.createCriteria().andUserIdEqualTo(userId);//.andGroupIdIsNotNull()
        List<GroupBuyUserRel> rels = groupBuyUserRelMapper.selectByExample(example);
        return rels;
    }
}
